package com.xs.ahoi.hcxs.handler;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.receiver.NotificationPlayReceiver;
import com.xs.ahoi.hcxs.service.BookFactory;
import com.xs.ahoi.hcxs.ui.activity.BookContentActivity;
import com.xs.ahoi.hcxs.utils.Constants;

/**
 * 播放通知
 *
 * @author fuhuichao
 * @date 2020-07-14 20:51
 */
public class NotifyPlayHandler extends BaseNotifyHandler {

    private volatile static NotifyPlayHandler instance;

    private String channel_id_normal = Ahoi.getInstance().getPackageName() + "_01";
    private String channel_name_normal = Ahoi.getInstance().getPackageName() + "_name_01";
    private int NOTIFY_ID_PLAY = 1000;

    // 设置按钮的点击事件
    private PendingIntent pi_prev = null;
    private PendingIntent pi_next = null;
    private PendingIntent pi_play = null;
    private PendingIntent pi_pause = null;
    private PendingIntent pi_close = null;

    private NotifyPlayHandler() {
    }

    public static NotifyPlayHandler getInstance() {
        if (instance == null) {
            synchronized (NotifyPlayHandler.class) {
                if (instance == null) {
                    instance = new NotifyPlayHandler();
                }
            }
        }
        return instance;
    }

    // 初始化
    public void init() {

        // 创建渠道
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationManager.deleteNotificationChannel(channel_id_normal);
            NotificationChannel channelNormal = new NotificationChannel(channel_id_normal, channel_name_normal, NotificationManager.IMPORTANCE_LOW);
            channelNormal.setSound(null, null);
            notificationManager.createNotificationChannel(channelNormal);
        }

        // 设置按钮的点击事件
        Context context = Ahoi.getInstance().getApplicationContext();
        pi_prev = PendingIntent.getBroadcast(context, 0, new Intent(NotificationPlayReceiver.ACTION_PREV), 0);
        pi_next = PendingIntent.getBroadcast(context, 0, new Intent(NotificationPlayReceiver.ACTION_NEXT), 0);
        pi_play = PendingIntent.getBroadcast(context, 0, new Intent(NotificationPlayReceiver.ACTION_PLAY), 0);
        pi_pause = PendingIntent.getBroadcast(context, 0, new Intent(NotificationPlayReceiver.ACTION_PAUSE), 0);
        pi_close = PendingIntent.getBroadcast(context, 0, new Intent(NotificationPlayReceiver.ACTION_CLOSE), 0);
    }

    // 更新播放的通知栏
    public void pushNotify() {
        if (pi_prev == null) {
            init();
        }

        // 设置点击notification的时候打开页面
        Context context = Ahoi.getInstance().getApplicationContext();
        Intent intent = new Intent(context, BookContentActivity.class);
        intent.putExtra(Constants.DEFAULT_ID, BookFactory.getInstance().getBookId());
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews remoteViews = new RemoteViews(Ahoi.getInstance().getPackageName(), R.layout.notify_play);
        remoteViews.setOnClickPendingIntent(R.id.btnPrev, pi_prev);
        remoteViews.setOnClickPendingIntent(R.id.btnNext, pi_next);
        remoteViews.setOnClickPendingIntent(R.id.ivClose, pi_close);

        // 设置书名和章节
        remoteViews.setTextViewText(R.id.tvTitle, bookFactory.getBookName());
        remoteViews.setTextViewText(R.id.tvCatalog, bookFactory.getCatalogName());

        // 设置播放和暂停按钮图片
        if (bookFactory.isPlaying()) {
            remoteViews.setImageViewResource(R.id.ivPlay, R.drawable.icon_black_pause);
            remoteViews.setOnClickPendingIntent(R.id.btnPlay, pi_pause);
        } else {
            remoteViews.setImageViewResource(R.id.ivPlay, R.drawable.icon_black_play);
            remoteViews.setOnClickPendingIntent(R.id.btnPlay, pi_play);
        }

        // 发起通知
        Notification notify = new NotificationCompat.Builder(context, channel_id_normal)
                .setSmallIcon(R.mipmap.ic_launcher_foreground)
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setCustomContentView(remoteViews)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(pi)
                .build();
        notify.flags |= Notification.FLAG_NO_CLEAR;
        notificationManager.notify(NOTIFY_ID_PLAY, notify);
    }

    // 取消播放通知
    public void cancelPlay() {
        super.cancelPlay(NOTIFY_ID_PLAY);
    }
}
